package com.avic.rong.course.activity;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.course.adapter.CompanysRvAdapter;
import com.avic.rong.course.adapter.DealRvAdapter;
import com.avic.rong.course.bean.CompanysBean;
import com.avic.rong.course.bean.DealBean;
import com.avic.rong.course.showapi.CompanysApi;
import com.avic.rong.course.showapi.DealApi;
import com.avic.rong.course.showapi.ToggleFollowApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DealActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.peixun_findexpert_rv)
    RecyclerView deal_rv;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private DealRvAdapter deal_rvAdapter;


    private DealApi dealApi;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_deal;
    }

    @Override
    protected void initView() {

        ButterKnife.bind(this);
        tvTitle.setText("交易信息");
//        ivRight.setImageResource(R.mipmap.gouwuche_dot);

        deal_rv.setLayoutManager(new LinearLayoutManager(this));
        deal_rvAdapter = new DealRvAdapter(this);
        deal_rv.setAdapter(deal_rvAdapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        initData();
    }


    private void initData() {
        dealApi = new DealApi(this, dealListener);
        Logger.i("-----dd------" + SharedPreferencesUtil.getLoginToken());
        dealApi.setArgument(SharedPreferencesUtil.getLoginToken(), getIntent().getIntExtra("skuId", 0), currentPageNumber, pageSize);
        HttpManager.getInstance().doHttpDeal(dealApi);
    }

    HttpOnNextListener<BaseResultEntity> dealListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (!baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_1000)) {
                ToastTools.showToast(context, baseResultEntity.getMsg());
                return;
            }

            List<DealBean> result = (List<DealBean>) baseResultEntity.getData();

            if (isRefresh) {
                deal_rvAdapter.getDataList().clear();
            }
            deal_rvAdapter.setDataList(result);

            if (deal_rvAdapter.getDataList() == null || deal_rvAdapter.getDataList().size() <= 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

        }


        @Override
        public void onError(Throwable e) {
            super.onError(e);
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
            Logger.e("cjx", "企业列表数据：" + e.getMessage());

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        currentPageNumber = 1;
        dealApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(dealApi);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        ++currentPageNumber;
        dealApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(dealApi);
    }


    @Override
    protected void addListener() {

    }
}
